/* 
 * File:   Device.cpp
 * Author: Aaron
 * 
 * Created on 20. Februar 2011, 11:15
 */


// -- Includes
#include "Device.hpp"
#include <CL/cl.hpp>


namespace OpenCLWrapping
{
    Device::Device(const cl::Device& _impl) :
        m_Impl(new cl::Device(_impl))
    {        
    }
  
    Device::Device(const Device& _orig) :
        m_Impl(new cl::Device(*_orig.getImpl()))
    {
    }

    Device::~Device()
    {
        delete m_Impl;
    }

    Device& Device::operator =(const Device& _other)
    {
        // -- Verify that's not a self-assignment
        if (this->getImpl() != _other.getImpl())
        {
            delete m_Impl;
            m_Impl = new cl::Device(*_other.getImpl());
        }

        return *this;
    }

    string Device::getName() const
    {
        string ret;
        if (this->getImpl()->getInfo(CL_DEVICE_NAME, &ret) != CL_SUCCESS)
        {
            ret = "";
        }
        
        return ret;
    }

    string Device::getDeviceVersion() const
    {
        string ret;
        if (this->getImpl()->getInfo(CL_DEVICE_VERSION, &ret) != CL_SUCCESS)
        {
            ret = "";
        }

        return ret;
    }

    string Device::getCLVersion() const
    {
        string ret;
        if (this->getImpl()->getInfo(
                CL_DEVICE_OPENCL_C_VERSION, &ret) != CL_SUCCESS)
        {
            ret = "";
        }

        return ret;
    }

    uint32 Device::getComputeUnits() const
    {
        cl_uint ret;
        if (this->getImpl()->getInfo(
                CL_DEVICE_MAX_COMPUTE_UNITS, &ret) != CL_SUCCESS)
        {
            ret = 0;
        }

        return uint32(ret);
    }

    uint32 Device::getClockFrequency() const
    {
        cl_uint ret;
        if (this->getImpl()->getInfo(
                CL_DEVICE_MAX_CLOCK_FREQUENCY, &ret) != CL_SUCCESS)
        {
            ret = 0;
        }

        return uint32(ret);
    }

    uint64 Device::getGlobalMemorySize() const
    {
        cl_ulong ret;
        if (this->getImpl()->getInfo(
                CL_DEVICE_GLOBAL_MEM_SIZE, &ret) != CL_SUCCESS)
        {
            ret = 0;
        }

        return uint64(ret);
    }

    cl::Device* Device::getImpl()
    {
        return m_Impl;
    }

    const cl::Device* Device::getImpl() const
    {
        return m_Impl;
    }
}

